﻿(function ($) {

    var defaults = {
        "paging": true, //是否分页
        "info": true, //是否显示分页信息
        "deferRender": true,//延迟渲染,提升大数据时的加载速度
        "pagingType": "full_numbers",
        "lengthChange": false,
        "searching": false,
        "processing": false,
        "serverSide": true,
        "ordering": false,
        "language": {
            "processing": "数据处理中...",
            "lengthMenu": "显示 _MENU_ 项结果",
            "zeroRecords": "没有匹配结果",
            "info": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "infoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "infoFiltered": "(由 _MAX_ 项结果过滤)",
            "infoPostFix": "",
            "search": "搜索:",
            "url": "",
            "emptyTable": " 没有查询到符合条件的数据",
            "loadingRecords": "数据载入中...",
            "infoThousands": ",",
            "paginate": {
                "first": "«",
                "previous": "‹",
                "next": "›",
                "last": "»"
            },
            "aria": {
                "sortAscending": ": 以升序排列此列",
                "sortDescending": ": 以降序排列此列"
            }
        },
        "ajax": null,
        "columns": null
    };

    var table = function () {
        return {
            DataTables: null,
            Search: function () {
                this.DataTables.ajax.reload();
            },
            RowData: function (i) {
                return this.DataTables.row(i).data();
            },
            SelectRowDatas: function () {
                return this.DataTables.rows(".selected").data();
            },
            SelectRow: function (obj) {
                $(obj).parents("tr").addClass("selected")
                $(obj)[0].checked = true
            },
            UnSelectRow: function (obj) {
                $(obj).parents("tr").removeClass("selected")
                $(obj)[0].checked = false;
            },
            Draw: function (bool) {
                this.DataTables.draw(bool);
            }
        };
    }



    $.fn.TaskTable = function (o) {
        $.fn.dataTable.ext.errMode = 'none';
        var options = $.extend({}, defaults, o);
        options.ajax["type"] = "POST";
        var _table = new table();
        _table.DataTables = $(this).on('error.dt', function (e, settings, techNote, message) {
            console.log('An error has been reported by DataTables: ', message);
        }).DataTable(options);
        return _table;
    };
})(jQuery);